using Sckf.MicroService.Services.Catalog.Domain.Repositories;

namespace Sckf.MicroService.Services.Catalog.Infrastructure.Repositories;

public partial class CatalogRepository : ICatalogRepository
{
    public async Task AddAsync(Domain.Entities.Catalog catalog)
    {
        await _context.Catalogs.AddAsync(catalog);
        await _context.SaveChangesAsync();
    }

    public async Task UpdateAsync(Domain.Entities.Catalog catalog)
    {
        _context.Update(catalog);
        await _context.SaveChangesAsync();
    }

    public async Task DeleteAsync(long catalogId)
    {
        var catalog = await _context.Catalogs.FirstOrDefaultAsync(c => c.Id == catalogId) ?? throw new ArgumentNullException("分类目录不存在");
        _context.Catalogs.Remove(catalog);
        await _context.SaveChangesAsync();
    }

    public async Task<Domain.Entities.Catalog> SingleAsync(long catalogId)
    {
        var catalog = await _context.Catalogs.FirstOrDefaultAsync(c => c.Id == catalogId) ?? throw new ArgumentNullException("分类目录不存在");
        return catalog;
    }

    public IQueryable<Domain.Entities.Catalog> Query(Expression<Func<Domain.Entities.Catalog, bool>> predicate)
    {
        return _context.Catalogs.Where(predicate);
    }
}

public partial class CatalogRepository
{
    private readonly CatalogDbContext _context;

    public CatalogRepository(CatalogDbContext context) => _context = context;
}
